package com.cn.springcloudwechat.common.utils;

import com.cn.springcloudwechat.model.YmUserSignIn;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {

    /**
     * 时间转换成时间戳
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 时间戳转时间(11位时间戳)
     * @param time
     * @return
     */
    public static String timestampToDate(long time) {
        String dateTime;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateTime = simpleDateFormat.format(new Date(time * 1000L));
        return dateTime;
    }

    /**
     * 获取与当前的时间差
     * @param ymUserSignIn
     * @return
     */
    public static long getTimeDifference(YmUserSignIn ymUserSignIn) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String now = df.format(new Date());// new Date()为获取当前系统时间
        //签到时间
        String signInTime = ymUserSignIn.getCreateTime();
        //计算连续签到
        Date d2 = df.parse(now);
        Date d1 = df.parse(signInTime);
        long diff = d2.getTime() - d1.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        return days;
    }



}
